package com.example.eos.service;

import com.example.eos.bean.Collec;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.bean.User;
import com.example.eos.mapper.CollecMapper;
import com.example.eos.mapper.PublishMapper;
import com.example.eos.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollecService {

    private CollecMapper collecMapper;
    private UserMapper userMapper;
    private PublishMapper publishMapper;

    @Autowired
    public CollecService(CollecMapper collecMapper, UserMapper userMapper, PublishMapper publishMapper){
        this.collecMapper = collecMapper;
        this.userMapper = userMapper;
        this.publishMapper = publishMapper;
    }

    //查询用户收藏
    public ReturnInfo getUserCollec(String userid){
        if(userMapper.findByUserid(userid)==null){
            return ReturnInfo.info2("用户不存在","");
        }else {
            return ReturnInfo.info2("成功",collecMapper.findByUserid(userid));
        }
    }

    //用户添加收藏
    public ReturnInfo userAddCollec(Collec collec, List<String> stringList){
        if(publishMapper.findByPublishid(collec.getPublishid())==null){
            stringList.add("作品不存在");
            return ReturnInfo.info2("失败", stringList);
        }else {
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            collec.setUserid(user.getUserid());
            collecMapper.addCollec(new Collec(collec));
            return ReturnInfo.info2("成功", stringList);
        }
    }

    //用户删除收藏
    public ReturnInfo userDeleteCollec(String collecid){
        String message = "成功";
        if(collecMapper.findByCollecid(collecid)==null){
            message = "失败";
        }else {
            collecMapper.deleteCollec(collecid);
        }
        return ReturnInfo.info1(message);
    }

}
